package com.example.leepong.Extensions;


import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.*;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.example.leepong.Game.Shapes.Coordinates;
import com.example.leepong.GameSurface;

import java.util.ArrayList;

public class SnappedHorizontalScrollView extends HorizontalScrollView {
    private GestureDetector _gestureDetector;

    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private ArrayList mItems = new ArrayList(3);
    private int mActiveFeature = 0;
    private int _childCount = 0;
    private int _width = 0;
    private int _visibleWidth = 0;
    private int _gapSize = 0;

    public SnappedHorizontalScrollView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);

        if(isInEditMode())
            return;

        _gestureDetector = new GestureDetector(this.getContext(), new SnappedHorizontalScrollView.MyGestureDetector());
        _gestureDetector.setIsLongpressEnabled(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

        this.setMeasuredDimension(parentWidth, parentHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        LinearLayout root = (LinearLayout) this.getChildAt(0);
        _childCount = root.getChildCount();
        _width = root.getWidth();
        _visibleWidth = getWidth();
        _gapSize = _width / _childCount;
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        super.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                // TODO Auto-generated method stub

                int scrollX = view.getScrollX();
                int scrollY = view.getScrollY();

                Log.i("Scrolling", "X from [" + scrollX + "] to [" + scrollY + "]");
                // scrollOne.scrollTo(scrollX, scrollY);
                return false;
            }

        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // _gestureDetector.onTouchEvent(ev);

        //If the user swipes
        if (_gestureDetector.onTouchEvent(ev)) {
            return true;
        } else if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
            int scrollX = getScrollX();
            int featureWidth = _gapSize;
            mActiveFeature = ((scrollX + (featureWidth / 2)) / featureWidth);
            int scrollTo = mActiveFeature * featureWidth;
            smoothScrollTo(scrollTo, 0);
            return true;
        } else {
            return super.onTouchEvent(ev);
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }

    public void ScrollToRight() {
        int scrollX = getScrollX();
        int itemNum = (scrollX / _gapSize);
        smoothScrollTo(_gapSize * (itemNum + 1), 0);
    }

    public void ScrollToLeft() {
        int scrollX = getScrollX();
        int itemNum = (int) ((scrollX - (_gapSize * 0.1)) / _gapSize);
        smoothScrollTo(_gapSize * (itemNum), 0);
    }

    public class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                //right to left
                //right to left
                if (velocityX < 0 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ScrollToRight();
                    return true;
                }
                //left to right
                else if (velocityX > 0 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    ScrollToLeft();
                    return true;
                }
            } catch (Exception e) {
                Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage());
            }
            return false;
        }
    }


}